package cn.com.sparkle.paxos.util;

import java.util.Comparator;

import org.apache.log4j.Logger;

import cn.com.sparkle.paxos.iomodel.protobuffmessage.PaxosMessages.Id;

public class IdComparator implements Comparator<Id> {
	private final static Logger logger = Logger.getLogger(IdComparator.class);
	private final static IdComparator instance = new IdComparator();

	public static IdComparator getInstance() {
		return instance;
	}

	private IdComparator() {
	}

	@Override
	public int compare(Id o1, Id o2) {
		int result;
		if (o1.getIncreaseId() == o2.getIncreaseId()) {
			result = o1.getAddress().compareTo(o2.getAddress());
			if (result != 0) {
				result = result > 0 ? 1 : -1;
			}
		} else {
			result = o1.getIncreaseId() > o2.getIncreaseId() ? 1 : -1;
		}

		return result;
	}

}
